package com.base.component.ai.platform.aliyun;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.base.component.ai.platform.ChatCompletionsHandler;
import com.base.component.ai.platform.core.bo.common.ChatCompletionsBO;
import com.base.component.ai.platform.core.bo.resp.ChatcompletionRespBO;
import com.base.component.ai.platform.core.bo.resp.ChatcompletionRespBO.MmChatcompletionV2ChoicesBO;
import com.base.core.head.dto.ai.MessageContentDataDTO;
import com.base.core.head.dto.ai.MessageListDTO;
import com.gitee.magic.context.ConverterEditorUtils;
import com.gitee.magic.core.json.JsonObject;
import com.gitee.magic.framework.base.rest.HttpRequest;
import com.gitee.magic.framework.base.rest.HttpWrapper;

import lombok.extern.slf4j.Slf4j;

/**
 * https://help.aliyun.com/zh/model-studio/user-guide/vision?spm=a2c4g.11186623.0.0.6e32453af8LkW0
 * @author zywei
 *
 */
@ConditionalOnProperty(
		prefix = "base.ai.aliyun.qwen",
		value = {"apiKey"})
@Slf4j
@Component
public class QianWenMessageContentDataHandler implements ChatCompletionsHandler<MessageListDTO> {

	@Value("${base.ai.aliyun.qwen.url:https://dashscope.aliyuncs.com}")
	private String url;
	@Value("${base.ai.aliyun.qwen.apiKey}")
	private String apiKey;

	@Override
	public List<String> getSupportModel() {
		return Arrays.asList(
				"qwen-vl-plus",
				//视觉理解
	    		"qwen-vl-max",
	    		//音频理解
	    		"qwen2-audio-instruct");
	}

	@Override
	public String execute(ChatCompletionsBO<MessageListDTO> param) {
		QianWenAO<MessageListDTO> ao=converterToParam(param);
		String requestbody=ConverterEditorUtils.converterObject(ao).toString();

		log.info("model:{} agent :{} request: {}",param.getModel(),param.getCode(),requestbody);
		HttpRequest request = new HttpRequest(url+"/compatible-mode/v1/chat/completions");
		request.setHeader(HttpWrapper.AUTHORIZATION, "Bearer " + apiKey);
		request.setHeader(HttpWrapper.CONTENTTYPE, HttpWrapper.CONTENTTYPE_JSON);
		request.setBodyContent(requestbody);
		HttpWrapper wrapper = new HttpWrapper();
		String content = wrapper.start(request);
		ChatcompletionRespBO v=ConverterEditorUtils.restoreObject(ChatcompletionRespBO.class, new JsonObject(content));
		if(!CollectionUtils.isEmpty(v.getChoices())) {
			for(MmChatcompletionV2ChoicesBO c:v.getChoices()) {
				return c.getMessage().getContent();
			}
		}
		return content;
	}

	@Override
	public void execute(ChatCompletionsBO<MessageListDTO> param, Consumer<String> process) {
		
	}
	
	public QianWenAO<MessageListDTO> converterToParam(ChatCompletionsBO<MessageListDTO> param) {
    	QianWenAO<MessageListDTO> ao=new QianWenAO<>();
		ao.setModel(param.getModel());
		ao.setMessages(param.getMessages());
		return ao;
    }
	
	public static void main(String[] args) {
		ChatCompletionsBO<MessageListDTO> param=new ChatCompletionsBO<>();
		param.setModel("qwen-vl-max");
		param.setTemperature(0.7F);
		MessageListDTO message=new MessageListDTO();
		message.setRole("user");
		message.setContent(Arrays.asList(
				MessageContentDataDTO.ofImage("https://inews.gtimg.com/om_bt/OE8piEBa-tbqn-wNvWZl8coi4AlzoUD43upEkoAnIkYL8AA/641"),
				MessageContentDataDTO.ofText("""
						#目标 
						结合给予的聊天上下文，识别上传图片里面的信息。 识别出来的信息将会被系统结合并运用到之后的反馈消息生成里。 
						#识别维度 
						1. 图片里面有什么 
						2. 图片传递了什么氛围 
						3. 根据上下文判断发图片的人想表达什么
							""")
				));
		param.setMessages(Arrays.asList(message));

		QianWenMessageContentDataHandler t=new QianWenMessageContentDataHandler();
		t.url="https://dashscope.aliyuncs.com";
		t.apiKey="sk-3dd5aca3d5a84db1a81a6dd69d9e7a87";
		System.out.println(t.execute(param));
	}
	
}
